import { message } from 'ant-design-vue';

export function useDownExcel() {
  function handleRes(res: any) {
    const url = window.URL.createObjectURL(new Blob([res.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'data.xlsx');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }
  function handleResWord(res: any) {
    const url = window.URL.createObjectURL(new Blob([res.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'data.xlsx');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }

  async function downMohthExcel() {
    await getMohthExcel().then((res: any) => {
      message.success('导出成功！');
      handleRes(res);
    });
  }
  async function downMohthExcelByDepartment(department: string) {
    await getMohthExcelByDepartment(department).then((res: any) => {
      message.success('导出成功！');
      handleRes(res);
    });
  }
  async function downKpiExcel() {
    await getKpiExcel().then((res: any) => {
      message.success('导出成功！');
      handleRes(res);
    });
  }
  async function downSummaryExcel() {
    await getSummaryExcel().then((res: any) => {
      message.success('导出成功！');
      handleRes(res);
    });
  }
  async function downExportWord() {
    await getExportWord().then((res: any) => {
      message.success('导出成功！');
      handleResWord(res);
    });
  }
  async function downExportWord1() {
    await getexportWord().then((res: any) => {
      message.success('导出成功！');
      handleResWord(res);
    });
  }
  async function downmonthhistoryexcel(month: string) {
    await getMonthhistoryExcel(month).then((res: any) => {
      message.success('导出成功！');
      handleResWord(res);
    });
  }
  async function downYunwanghistoryexcel(month: string) {
    await getYunWanghistoryExcel(month).then((res: any) => {
      message.success('导出成功！');
      handleResWord(res);
    });
  }
  async function downQuarterhistoryexcel(month: string) {
    await getQuarterhistoryExcel(month).then((res: any) => {
      message.success('导出成功！');
      handleResWord(res);
    });
  }
  return {
    downMohthExcel,
    downMohthExcelByDepartment,
    downKpiExcel,
    downSummaryExcel,
    downExportWord,
    downExportWord1,
    downmonthhistoryexcel,
    downYunwanghistoryexcel,
    downQuarterhistoryexcel,
  };
}
